package com.zye.dao;


import com.zye.Util.JDBCUtils;
import com.zye.dao.StudentDAO;
import com.zye.entity.Student;

import java.util.List;

public class StudentDAOImpl implements StudentDAO {
    @Override
    public List<Student> selectAllStudents() {
       List<Student> student= JDBCUtils.query(Student.class,"select * from student");
       if(student.size()>0){
           return student;
       }
       return null;
    }

    @Override
    public List<Student> selectStudentByWord(String word) {
        List<Student> student = JDBCUtils.query(Student.class,"select * from student where name like ?","%"+word+"%");
        if (student.size()>0){
            return student;
        }
        return null;
    }

    @Override
    public Student selectStudentById(int id) {
        Student student =JDBCUtils.query(Student.class,"select * from student where id = ?",id).get(0);
        if (student!=null){
            return student;
        }
        return null;
    }

    @Override
    public void insertStudent(Student student) {
        String sql = "insert into student(name,age,gender,address,idcard) values(?,?,?,?,?)";
        JDBCUtils.update(sql,student.getName(),student.getAge(),student.getGender(),student.getAddress(),student.getIdcard());
    }

    @Override
    public void updateStudentById(Student student) {
        JDBCUtils.update("update student set name = ?,age = ?,gender=?,address = ?,idcard = ? where id = ?",student.getName(),student.getAge(),student.getGender(),student.getAddress(),student.getIdcard());
    }

    @Override
    public void deleteStudentById(int id) {
        JDBCUtils.update("delete from student where id = ?",id);
    }
}

